#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int uniquePaths(int m, int n) {
    int dr[2] = { 1, 0 }, dc[2] = { 0, 1 };
    vector<vector<int>> dp(m, vector<int>(n, 0));
    vector<vector<bool>> vis(m, vector<bool>(n, false));
    dp[0][0] = 1;
    vis[0][0] = true;

    queue<pair<int, int>> q;
    q.push({ 0, 0 });

    while (!q.empty()) {
        auto [r, c] = q.front();
        q.pop();
        for (int i = 0; i < 2; i++) {
            int nr = r + dr[i], nc = c + dc[i];
            if (nr >= 0 && nr < m && nc < n && nc >= 0) {
                dp[nr][nc] += dp[r][c];
                if (!vis[nr][nc]) {
                    q.push({ nr, nc });
                    vis[nr][nc] = true;
                }
            }
        }
    }

    return dp[m - 1][n - 1];
}

int uniquePaths(int m, int n) {
    vector<vector<int>> dp(m, vector<int>(n, 0));
    for (int i = 0; i < m; i++) {
        dp[i][0] = 1;
    }
    for (int j = 0; j < n; j++) {
        dp[0][j] = 1;
    }

    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
        }
    }
    return dp[m - 1][n - 1];
}

int main() {
	int m = 3, n = 7;
	cout << uniquePaths(m, n) << endl;
	return 0;
}